package com.example.hadoop.mapreduce.dbscan;

import org.apache.hadoop.fs.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DbscanUtils {
    public static void writeParameters(FileSystem fileSystem, String dbscanParamPath, double epsilon, int minPoints) throws IOException {
        FSDataOutputStream outputStream = fileSystem.create(new Path(dbscanParamPath));
        outputStream.writeUTF(String.format("%f,%d\n", epsilon, minPoints));
        outputStream.flush();
        outputStream.close();
    }

    public static String[] readParameters(FileSystem fileSystem, String dbscanParamPath) throws IOException {
        String[] result = null;
        FSDataInputStream inputStream = fileSystem.open(new Path(dbscanParamPath));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = bufferedReader.readLine();
        bufferedReader.close();
//        inputStream.close();
        result = line.split(",");
        return result;
    }

    public static double getEps(String[] temp) {
        return Double.valueOf(temp[0]);
    }

    public static int getMinPoints(String[] temp) {
        return Integer.valueOf(temp[1]);
    }
}
